# Adogs
# 时间: 2021/8/15 11:35
import enum

import pygame

from dialog.actor_dialog import TieTouDialog, GuoQiangDialog
from dialog.tunnel_dialog import TunnelTask3BeginDialog, TunnelTask3EndDialog, TunnelTaskFailDialog
from scene.task import BaseTask, TaskStatus


class TransmitMessageTaskStatus(enum.IntEnum):
    """
    任务状态
    """
    finding_guo_qiang = 0
    found_guo_qiang = 1
    finding_tie_tou = 2
    found_tie_tou = 3
    success = 4  # task to complete
    fail = 5  # task failure


class TransmitMessageTask(BaseTask):
    """
    任务三:传递消息
    方法:
    1.__init__(xiao_tie, guo_qiang, tie_tou, font)
    2.draw_task(current_surface, view_x, view_y)
    3.do_self_task(key_down, key_list)
    4.__do_collide_actor(actor)
    """
    def __init__(self, xiao_tie, guo_qiang, tie_tou, font):
        """
        初始化函数
        :param xiao_tie: 主角人物
        :param guo_qiang: 红军_国强
        :param tie_tou: 红军_铁头
        :param font: 字体
        """
        self.guo_qiang = guo_qiang
        self.tie_tou = tie_tou
        self.xiao_tie = xiao_tie

        self.time_count = 100000    # 计时器

        self.state = TransmitMessageTaskStatus.finding_guo_qiang    # 任务状态初始化
        self.tie_tou_dialog = TieTouDialog(self.tie_tou, font)  # 初始化人物对话
        self.guo_qiang_dialog = GuoQiangDialog(self.guo_qiang, font)    # 初始化人物对话

        self.start_dialog = TunnelTask3BeginDialog(font)
        self.win_dialog = TunnelTask3EndDialog(font)
        self.fail_dialog = TunnelTaskFailDialog(font)
        super(TransmitMessageTask, self).__init__(self.start_dialog, self.win_dialog, self.fail_dialog)

    def draw_task(self, current_surface, view_x, view_y):
        """
        任务绘制函数
        :param current_surface: 绘制屏幕
        :param view_x: 屏幕相对x坐标
        :param view_y: 屏幕相对y坐标
        :return: 无返回值
        """
        self.guo_qiang.draw(current_surface, view_x, view_y)    # 绘制国强

        if self.state == TransmitMessageTaskStatus.found_guo_qiang:
            self.guo_qiang_dialog.draw(current_surface, view_x, view_y)
            if self.guo_qiang_dialog.is_finish():
                self.state = TransmitMessageTaskStatus.finding_tie_tou

        elif self.state == TransmitMessageTaskStatus.found_tie_tou:
            self.tie_tou_dialog.draw(current_surface, view_x, view_y)
            if self.tie_tou_dialog.is_finish():
                self.state = TransmitMessageTaskStatus.success
                self.task_status = TaskStatus.win_dialog

        if self.state > TransmitMessageTaskStatus.found_guo_qiang:
            self.tie_tou.draw(current_surface, view_x, view_y)

    def do_self_task(self, key_down, key_list):
        """
        任务函数
        :param key_down: 判断是否点击按键
        :param key_list: 判断是否有键被按下
        :return: 无返回值
        """

        if key_list[pygame.K_z]:
            self.state = TransmitMessageTaskStatus.success
            self.task_status = TaskStatus.win_dialog
            self.xiao_tie.set_status("shoot")

        if self.state == TransmitMessageTaskStatus.finding_guo_qiang:
            if self.__do_collide_actor(self.guo_qiang):
                self.state = TransmitMessageTaskStatus.found_guo_qiang

        elif self.state == TransmitMessageTaskStatus.finding_tie_tou:
            if self.__do_collide_actor(self.tie_tou):
                self.state = TransmitMessageTaskStatus.found_tie_tou

        elif self.state == TransmitMessageTaskStatus.found_guo_qiang:
            if key_down and key_list[pygame.K_SPACE]:
                self.guo_qiang_dialog.next_text()
            if self.guo_qiang_dialog.is_finish():
                self.state = TransmitMessageTaskStatus.finding_tie_tou

        elif self.state == TransmitMessageTaskStatus.found_tie_tou:
            if key_down and key_list[pygame.K_SPACE]:
                self.tie_tou_dialog.next_text()
            if self.tie_tou_dialog.is_finish():
                self.state = TransmitMessageTaskStatus.success
                self.task_status = TaskStatus.win_dialog
                self.xiao_tie.set_status("shoot")

        self.time_count -= 1
        if self.time_count <= 0:
            self.task_status = TaskStatus.fail_dialog

    def __do_collide_actor(self, actor):
        """
        人物碰撞判断函数
        :param actor: 判断碰撞的人物
        :return: 无返回值
        """
        temp_rect = self.xiao_tie.rect
        self.xiao_tie.rect = pygame.Rect(self.xiao_tie.pos_x + 5,
                                         self.xiao_tie.pos_y + 5, 20, 60)
        is_collide = pygame.sprite.collide_rect(self.xiao_tie, actor)
        self.xiao_tie.rect = temp_rect
        if is_collide:
            return True
